%%-*- mode: erlang -*-

{mapping, "emqx_persistence_plugin.enable_persistence", "emqx_persistence_plugin.enable_persistence", [
  {datatype, flag},
  {default, off}
]}.

{mapping, "emqx_persistence_plugin.hook.client.connected.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.client.disconnected.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.client.subscribe.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.client.unsubscribe.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.session.subscribed.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.session.unsubscribed.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.message.publish.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.message.acked.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

{mapping, "emqx_persistence_plugin.hook.message.delivered.$name", "emqx_persistence_plugin.hooks", [
  {datatype, string}
]}.

%%---------------------------------------------------------------------------------------------------------

{translation, "emqx_persistence_plugin.hooks", fun(Conf) ->
  Hooks = cuttlefish_variable:filter_by_prefix("emqx_persistence_plugin.hook", Conf),
  lists:map(
    fun({[_, _, Name1, Name2, _], Val}) ->
	  {lists:concat([Name1, ".", Name2]), Val}
	end, Hooks)
end}.

{mapping, "persistence.mysql.server", "emqx_persistence_plugin.server", [
  {default, {"127.0.0.1", 3306}},
  {datatype, [integer, ip, string]}
]}.

{mapping, "persistence.mysql.pool", "emqx_persistence_plugin.server", [
  {default, 8},
  {datatype, integer}
]}.

{mapping, "persistence.mysql.username", "emqx_persistence_plugin.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "persistence.mysql.password", "emqx_persistence_plugin.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "persistence.mysql.database", "emqx_persistence_plugin.server", [
  {default, "mqtt"},
  {datatype, string}
]}.

{mapping, "persistence.mysql.query_timeout", "emqx_persistence_plugin.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "persistence.mysql.ssl", "emqx_persistence_plugin.server", [
  {default, off},
  {datatype, flag}
]}.

{mapping, "persistence.mysql.ssl.cafile", "emqx_persistence_plugin.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "persistence.mysql.ssl.certfile", "emqx_persistence_plugin.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "persistence.mysql.ssl.keyfile", "emqx_persistence_plugin.server", [
  {default, ""},
  {datatype, string}
]}.

{translation, "emqx_persistence_plugin.server", fun(Conf) ->
  {MyHost, MyPort} =
  case cuttlefish:conf_get("persistence.mysql.server", Conf) of
    {Ip, Port} -> {Ip, Port};
    S          -> case string:tokens(S, ":") of
                    [Domain]       -> {Domain, 3306};
                    [Domain, Port] -> {Domain, list_to_integer(Port)}
                  end
    end,
  Pool = cuttlefish:conf_get("persistence.mysql.pool", Conf),
  Username = cuttlefish:conf_get("persistence.mysql.username", Conf),
  Passwd = cuttlefish:conf_get("persistence.mysql.password", Conf),
  DB = cuttlefish:conf_get("persistence.mysql.database", Conf),
  Timeout = case cuttlefish:conf_get("persistence.mysql.query_timeout", Conf) of
                "" -> 300000;
                Duration ->
                    case cuttlefish_duration:parse(Duration, ms) of
                      {error, Reason} -> error(Reason);
                      Ms when is_integer(Ms) -> Ms
                  end
            end,
  Options = [{pool_size, Pool},
             {auto_reconnect, 1},
             {host, MyHost},
             {port, MyPort},
             {user, Username},
             {password, Passwd},
             {database, DB},
             {encoding, utf8},
             {query_timeout, Timeout},
             {keep_alive, true}],
  Enable = cuttlefish:conf_get("enable_persistence.enable", Conf, off),
  case cuttlefish:conf_get("persistence.mysql.ssl", Conf) of
        true ->
            CA = cuttlefish:conf_get("persistence.mysql.ssl.cafile", Conf),
            Cert = cuttlefish:conf_get("persistence.mysql.ssl.certfile", Conf),
            Key = cuttlefish:conf_get("persistence.mysql.ssl.keyfile", Conf),
            NewsOptions = Options ++ [{ssl, [{server_name_indication, disable},
                                             {cacertfile, CA},
                                             {certfile, Cert},
                                             {keyfile, Key}]}],
            [{enable, Enable},{options, NewsOptions}];
        _ ->
            [{enable, Enable},{options, Options}]
  end
end}.

